package com.ztc;

import com.common.Constant;
import com.common.Lession;
import com.common.NPLession;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by ztc on 16-1-1.下午10:13
 */
public class Tools {
    public static Matcher patternScore(String score) {
        String reg="\"kcmc\":\"(.*?)\",\"jsxm\":\"(.*?)\",\"xq\":\"(.*?)\",\"xs\":\"(.*?)\",\"xf\":\"(.*?)\",\"zpcj\":\"(.*?)\",\"pscj\":\"(.*?)\",\"qmcj\":\"(.*?)\",\"kcsx\":\"(.*?)\",\"cjid\":\"(.*?)\",\"ksfsm\":\"(.*?)\",\"pxcj\":\"(.*?)\"}";
        Pattern p= Pattern.compile(reg);
        return p.matcher(score);
    }
    public static Matcher patternName(String name) {
        String reg="<b>(.*?)</b>";
        Pattern p=Pattern.compile(reg);
        return p.matcher(name);
    }
    public static Matcher patternCjid(String cjid){
        String reg="\"cjid\":\"(.*?)\",\"xq\":\"(.*?)\",\"pjzblbm\":\"(.*?)\",\"kch\":\"(.*?)\",\"kcmc\":\"(.*?)\",\"jsxm\":\"(.*?)\",\"fzjsxm\":\"(.*?)\",\"cpjg\":\"(.*?)\",\"ksfs\":\"(.*?)\",\"kcsx\":\"(.*?)\",\"ksxz\":\"(.*?)\"";
        Pattern p=Pattern.compile(reg);
        return p.matcher(cjid);
    }
    public static List<NPLession> getNPLession(Matcher cjid){
        List<NPLession> les=new ArrayList<NPLession>();
        //可能是cpjg判断是否评过
        while(cjid.find()){
            les.add(new NPLession(cjid.group(1),cjid.group(2),cjid.group(3),cjid.group(4),
                    cjid.group(5),cjid.group(6),cjid.group(7),cjid.group(8),cjid.group(9),
                    cjid.group(10),cjid.group(11)));
        }
        return les;
    }
    public static boolean isNumber(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }
    public static List<Lession> getJidian(Matcher score){
        List<Lession> les=new ArrayList<Lession>();
        while(score.find()) {
            double	xf=0.0;
            if(score.group(11).equals("3")) {
                if (score.group(6).equals("优"))                //五级计分
                    xf = Double.valueOf(score.group(5)) * 4.5;
                else if (score.group(6).equals("良"))
                    xf = Double.valueOf(score.group(5)) * 3.5;
                else if (score.group(6).equals("中"))
                    xf = Double.valueOf(score.group(5)) * 2.5;
                else if (score.group(6).equals("及格"))
                    xf = Double.valueOf(score.group(5)) * 1.5;
            }else if(score.group(11).equals("1")&&isNumber(score.group(6))) {
                if (Double.valueOf(score.group(6)) >= 90)                    //百分计分
                    xf = ((Double.valueOf(score.group(6)) - 90) / 10 + 4.0) * Double.valueOf(score.group(5));
                else if (Double.valueOf(score.group(6)) >= 80 && Double.valueOf(score.group(6)) <= 89)
                    xf = ((Double.valueOf(score.group(6)) - 80) / 10 + 3.0) * Double.valueOf(score.group(5));
                else if (Double.valueOf(score.group(6)) >= 70 && Double.valueOf(score.group(6)) <= 79)
                    xf = ((Double.valueOf(score.group(6)) - 70) / 10 + 2.0) * Double.valueOf(score.group(5));
                else if (Double.valueOf(score.group(6)) >= 60 && Double.valueOf(score.group(6)) <= 69)
                    xf = ((Double.valueOf(score.group(6)) - 60) / 10 + 1.0) * Double.valueOf(score.group(5));
            }
            les.add(new Lession(score.group(1),score.group(2),score.group(3),score.group(4),score.group(5),score.group(6),score.group(7),score.group(8),score.group(9),score.group(10),score.group(11),score.group(12),xf));
        }
        return les;
    }
    public static Connection getDBcon(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection dbcon= DriverManager.getConnection(Constant.mysqlUrl,Constant.mysqlUser,Constant.mysqlPW);
            return dbcon;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static int saveInfo(String xh,String name,String sfzh,String pw,String yx,String bj){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection dbcon= DriverManager.getConnection(Constant.mysqlUrl,Constant.mysqlUser,Constant.mysqlPW);
            //BaseInfo(xh varchar(15),name varchar(10),sfzh varchar(20),pw varchar(20),yx varchar(30),bj varchar(20));
            String selectSql="select*from BaseInfo where xh='"+xh+"';";
            String selectSql1="select*from BaseInfo where xh='"+xh+"' and name='"+name+"' and pw='"+pw+"';";
            String insertSql = "insert into BaseInfo(xh,name,sfzh,pw,yx,bj) values(?,?,?,?,?,?)";
            PreparedStatement pstmt = dbcon.prepareStatement(insertSql);
            Statement stmt=dbcon.createStatement();
            ResultSet result=stmt.executeQuery(selectSql);
            if(!result.next()) {
                pstmt.setString(1, xh);
                pstmt.setString(2, name);
                pstmt.setString(3, sfzh);
                pstmt.setString(4, pw);
                pstmt.setString(5, yx);
                pstmt.setString(6, bj);
                //影响行数
                int r = pstmt.executeUpdate();
                pstmt.close();
                stmt.close();
                dbcon.close();
                return r;
            }else{
                ResultSet result1=stmt.executeQuery(selectSql1);
                if(!result1.next()) {
                    String updateSql = "update BaseInfo set name='" + name + "',sfzh='"
                            + sfzh + "',pw='" + pw + "',yx='" + yx + "',bj='" + bj + "' where xh='"+xh+"';";
                    int r = stmt.executeUpdate(updateSql);
                    pstmt.close();
                    stmt.close();
                    dbcon.close();
                    return r;
                }
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }
}
